package com.laoniu.biz.base;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.laoniu.dto.PageResponse;
import com.laoniu.dto.RestResult;
import com.laoniu.dto.request.BaseCustomerRequestDto;
import com.laoniu.dto.response.BaseCustomerDto;
import com.laoniu.dto.response.BaseSupplierDto;
import com.laoniu.entity.BaseCustomer;
import com.laoniu.entity.SecurityUser;
import com.laoniu.service.base.BaseCustomerService;
import com.laoniu.utils.SpringSecurityUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class BaseCustomBiz {

    @Autowired
    private BaseCustomerService customerService;

    /**
     * 分页查询
     * @param requestDto
     * @return
     */
    public PageResponse<BaseCustomerDto> pageList(BaseCustomerRequestDto requestDto) {
        Page<BaseCustomer> page = customerService.listPage(requestDto);
        PageResponse<BaseCustomerDto> result = new PageResponse<BaseCustomerDto>();
        result.setPageNo(requestDto.getPageNo());
        result.setPageSize(requestDto.getPageSize());
        result.setTotal(page.getTotal());
        if (CollectionUtils.isNotEmpty(page.getRecords())){
            List<BaseCustomerDto> resultList = Lists.newArrayList();
            for (BaseCustomer record : page.getRecords()) {
                BaseCustomerDto convert = Convert.convert(BaseCustomerDto.class, record);
                resultList.add(convert);
            }
            result.setData(resultList);
        }
        return result;
    }

    /**
     * 添加客户
     * @param dto
     */
    public void addCustomer(BaseSupplierDto dto) {
        BaseCustomer customer = Convert.convert(BaseCustomer.class, dto);
        SecurityUser user = SpringSecurityUtils.getUser();
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        customer.setOpId(user.getId());
        customer.setOpName(user.getName());
        customerService.save(customer);
    }

    /**
     * 更新客户
     * @param dto
     */
    public void updateCustomer(BaseSupplierDto dto) {
        if (Objects.isNull(dto.getId())){
            return;
        }
        BaseCustomer customer = customerService.getById(dto.getId());
        if (Objects.isNull(customer)) return;
        SecurityUser user = SpringSecurityUtils.getUser();
        customer.setName(dto.getName());
        customer.setAddress(dto.getAddress());
        customer.setPhone(dto.getPhone());
        customer.setEmail(dto.getEmail());
        customer.setPostCode(dto.getPostCode());
        customer.setContactNumber(dto.getContactNumber());
        customer.setContactPerson(dto.getContactPerson());
        customer.setBank(dto.getBank());
        customer.setBankNum(dto.getBankNum());
        customer.setStatus(dto.getStatus());
        customer.setUpdateTime(LocalDateTime.now());
        customer.setOpId(user.getId());
        customer.setOpName(user.getName());
        customerService.updateById(customer);
    }


    public RestResult delete(Long id) {
        if (Objects.isNull(id)){
            return RestResult.fail("id不能为空");
        }
        BaseCustomer customer = customerService.getById(id);
        if (Objects.isNull(customer)){
            return RestResult.fail("id无效");
        }
        SecurityUser user = SpringSecurityUtils.getUser();
        if (!user.getId().equals(customer.getOpId())) {
            return RestResult.fail("您不能删除该客户");
        }
        customerService.removeById(id);
        return RestResult.success();
    }

    public List<BaseCustomerDto> listAll() {
        LambdaQueryWrapper<BaseCustomer> wrapper = Wrappers.lambdaQuery(BaseCustomer.class);
        wrapper.orderByDesc(BaseCustomer::getId);
        List<BaseCustomer> list = customerService.list(wrapper);
        List<BaseCustomerDto> result = Lists.newArrayList();
        if (CollectionUtils.isEmpty(list)){
            return result;
        }
        for (BaseCustomer customer : list) {
            BaseCustomerDto dto = Convert.convert(BaseCustomerDto.class, customer);
            result.add(dto);
        }
        return result;
    }
}
